<?
include "db.php";
$query = mysql_query("SELECT id FROM docs LIMIT 1;", $connection);
if (@ mysql_num_rows($query) != 1) $first = true; else $first = false;

if ((!isset($_GET['p']) || $_GET['p'] == "index.html") && !$first)
{
?>
<html>
	<head>
		<link rel="stylesheet" type="text/css" href="./styles.css">
		<title>PHPSearchDocs</title>
	</head>
	<body>
	<form action="index.php" method="GET">
		<b>SEARCH</b><br />
		Search Type:
		<select name='t'>
			<option value='text'>Full Text Search (slower)</option>
			<option value='file'>Keyword Search (very fast)</option>
		</select>&nbsp;&nbsp;
		Keywords (separated by spaces): <input type="text" name="k" />&nbsp;<input type="submit" value='go' />
	</form>
<? 
	if (isset($_GET['k'])) 
	{	
		$keywords = explode(" ", $_GET['k']);
		foreach ($keywords as $k => $v) { $keywords[$k] = mysql_real_escape_string($v, $connection); }
		
		if ($_GET['t'] == 'file')
		{
			$query = "SELECT * FROM docs WHERE";
			foreach ($keywords as $v) { $query .= " name LIKE '%" . $v . "%' AND"; }
			$query .= " 1=1 ORDER BY docs.name;";
		} else {
			$match = implode(",", $keywords);
			$query = "SELECT id, name, file, (MATCH (text) AGAINST ('{$match}')) AS score FROM docs WHERE MATCH (text) AGAINST ('{$match}') ORDER BY score DESC;";
		}
	} else {
		$query = "SELECT * FROM docs;";
	}
	$q = mysql_query($query, $connection);
?>
	<table>
		<th>
			<td class='results'>docname</td>
			<td class='results'>filename</td>
			<? if ($_GET['t'] == 'text') { ?>
			<td class='results'>match</td>
			<? } ?>
		</th>
<?
	while ($r = mysql_fetch_array($q))
	{
?>
		<tr>
			<td class='results'><? print $r['id']; ?></td>
			<td class='results'><? print $r['name']; ?></td>
			<td class='results'><a href="index.php?p=<? print $r['file']; ?>"><? print $r['file']; ?></a></td>
			<? if ($_GET['t'] != 'file') { ?>
			<td class='results'><? print substr($r['score'], 0, 5); ?></td>
			<? } ?>
		</tr>
<?
	}
?>
	</table>
	</body>
</html>
<? } elseif ($first) { ?>
<html>
	<head>
		<link rel="stylesheet" type="text/css" href="./styles.css">
		<title>PHPSearchDocs</title>
	</head>
	<body>
		Before using this utility, you need to:<br />
		<ul>
			<li>Make sure that your PHP Documentation is in a subfolder of this directory named "html".</li>
			<li>Make sure you have access to a MySQL database.</li>
			<li>Set the host, username, password and database values in db.php</li>
			<li>Download <a href='index-files.php'>this file</a>, then run the command "<i>mysql < FILENAME</i>", replacing FILENAME with the name of the file you saved it as. That command may take a while to complete.</li>
			<li>Refresh this page once you've done all that.</li>
			<li>Now you're ready to use the PHP Documentation searcher!</li>
		</ul>
	</body>
</html>
<? } elseif (isset($_GET['p'])) {
	if (substr($_GET['p'], 0, 7) == "http://") header("Location: {$_GET['p']}");
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="./styles.css">
<?
	$page = file_get_contents("./html/" . $_GET['p']);
	$rmto = strpos($page, "<TITLE");
	$page = substr(str_replace('HREF="', 'HREF="index.php?p=', $page), $rmto, strlen($page) - $rmto);
	print $page;
}
?>